package com.example.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.common.AutoLog;
import com.example.common.Result;
import com.example.entity.Devices;
import com.example.entity.Params;
import com.example.service.DeviceValueUpdate;
import com.example.service.DevicesService;
import com.example.service.KafkaProducerService;
import com.example.util.JacksonUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("Devices")
public class DevicesController {

    @Resource
    private DevicesService devicesService;
    @Resource
    private KafkaProducerService kafkaProducerService;

    @GetMapping("/findAllDevices")
    public Result findAll() {
        List<Devices> devices = devicesService.findAll();
        return Result.success(devices);
    }

    @GetMapping("/findBySearchOfDevices")
    public Result findBySearchOfDevices(Params params) {
        PageInfo<Devices> search = devicesService.findBySearchOfDevices(params);
        return Result.success(search);
    }

    //新增和编辑共用一个请求路径，首先根据deviceId查询数据库
    //若数据库为空，则进行新增操作，否则进行修改操作
    @PostMapping("/addDevice")
    @AutoLog("修改设备信息")
    public Result addDevice(@RequestBody Devices device) {
        log.info("前端发来的设备信息：" + device);
        Devices byDeviceId = devicesService.findByDeviceId(device.getDeviceId());
        if (byDeviceId == null) {
            devicesService.addDevice(device);
        } else {
            devicesService.updateDevice(device);
        }
        if (ObjectUtil.equal(device.getControlMode(), "M")) {
            //通过设备类型创建修改设备状态的接口方法
            DeviceValueUpdate valueUpdateService = DevicesService.getValueUpdate(device.getDeviceType());
            valueUpdateService.statusUpdate(device.getStatus());
            KafkaProducerService.MessageBody current = KafkaProducerService.MessageBody.newInstance();
            kafkaProducerService.sendMessage(current);
            log.info("message has been sent through 'M' mode, detail: {}", JacksonUtil.encode(current));
        }
        return Result.success();
    }

    @Scheduled(fixedRate = 5000)
    public void schedule() {
        List<Devices> deviceListInAutoMode = devicesService.list(
                Wrappers.<Devices>lambdaQuery()
                        .eq(Devices::getControlMode, "A"));
        devicesService.superviseDeviceInAutoMode(deviceListInAutoMode);
    }

    @DeleteMapping("/deleteDevice/{deviceId}")
    @AutoLog("删除设备信息")
    public Result deleteDevice(@PathVariable String deviceId) {
        devicesService.removeDevice(deviceId);
        return Result.success();
    }

    @GetMapping("/echarts/pie")
    public Result pie() {
        List<Map<String, Object>> list = devicesService.initPie();
        log.info(String.valueOf(list));
        return Result.success(list);
    }

}
